<?php 

class Twitter {
	
	private $CI;
	private $twitter_url = 'http://twitter.com';
	private $username = '';
	private $password = '';
	private $curl_connection;
	private $format = 'xml';
	private $response = false;
	private $responseData = false;
	
	public function Twitter($params) {
		$this->__construct($params);
	}
	
	public function __construct($params) {
		
		$this->CI =& get_instance();
		
		if (isset($params['username'])) $this->username = $params['username'];
		if (isset($params['password'])) $this->password = $params['password'];
		if (isset($params['format'])) $this->format = $params['format'];
		
		$this->curl_connection = curl_init();
	
		curl_setopt($this->curl_connection, CURLOPT_VERBOSE, 1);
		curl_setopt($this->curl_connection, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->curl_connection, CURLOPT_USERPWD, $this->username.":".$this->password);
		curl_setopt($this->curl_connection, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
		curl_setopt($this->curl_connection, CURLOPT_POST, 1);
    }
    
    public function __destruct() {
		/*clean Up */
		curl_close($this->curl_connection);
	}
	
	public function update($status) {
	
		if(strlen($stat) < 1) return false; 
		
		/*Set the host information for the curl session */
		$target_url = $this->twitter_url . "/statuses/update.".$this->format."?status=". urlencode(stripslashes(urldecode($stat)));
		
		return $this->sendRequest($target_url);
	}
	
	public function updateDirect($targetuser, $message) {
		if(strlen($message) < 1) return false; 
		
		/*Set the host information for the curl session */
		$target_url = $this->twitter_url . "/statuses/update.".$this->format."?status=". urlencode(stripslashes(urldecode('d '.$targetuser.' '.$message)));
		
		return $this->sendRequest($target_url);
	}
	
	public function publicTimeline($sinceId = false) {
		$target_url = $this->twitter_url . "/statuses/public_timeline.".$this->format."?";
		
		if ($sinceId) $target_url .= "since_id=". urlencode(stripslashes(urldecode($sinceId)));

		return $this->sendRequest($target_url);
	}
	
	public function friendsTimeline($friendId = false, $since = false, $page = false ) {
		$target_url = $this->twitter_url . "/statuses/public_timeline.".$this->format."?";
		
		$arguments = '';
		
		if ($friendId) {
			if (strlen($arguments) ) $arguments .= '&';
			$arguments .= "id=". urlencode(stripslashes(urldecode($friendId)));
		}
		if ($since) {
			if (strlen($arguments) ) $arguments .= '&';
			$arguments .= "since=". urlencode(stripslashes(urldecode($since)));
		}
		if ($page) {
			if (strlen($arguments) ) $arguments .= '&';
			$arguments .= "page=". urlencode(stripslashes(urldecode($page)));
			
		}

		return $this->sendRequest($target_url.$arguments);
	}
	
	public function getResponse() {
		return $this->response;
	}
	
	public function getResponseData() {
		return $this->responseData;
	}
    
    private function sendRequest($target_url) {
    	curl_setopt($this->curl_connection, CURLOPT_URL, $target_url);
		
		/* Execute and get the http code to see if it was succesfull or not*/
		$response = curl_exec($this->curl_connection);	
		$responseData = curl_getinfo($this->curl_connection);
		
		$this->response = $response;
		$this->responseData = $responseData;
		
		if ($responseData['http_code'] == 200) return true;
		else return false;
    }
}

?>